/////////////////////////////////////////////////////////////////////////////////////////
// Luminary Micro / Circuit Cellar 2007 Design Contest
// Entry LM1732
//
// SHT11 driver
// Taken from Sensirion example code (http://www.sensirion.com)
/////////////////////////////////////////////////////////////////////////////////////////

#ifndef _SHT11IF


////////////////////////////////////////////////////////////////////////////
// CONSTANTS
////////////////////////////////////////////////////////////////////////////
#define noACK	0
#define ACK		1

#define SHT11PORT	GPIO_PORTE_BASE		// SHT11 port
#define	SHT_SCK		GPIO_PIN_0			// Clock Line
#define	SHT_DATA	GPIO_PIN_1			// Data Line

								// adr  command  r/w
#define STATUS_REG_W	0x06	// 000    0011    0
#define STATUS_REG_R	0x07	// 000    0011    1
#define MEASURE_TEMP	0x03	// 000    0001    1
#define MEASURE_HUMI	0x05	// 000    0010    1
#define RESET			0x1e	// 000    1111    0


////////////////////////////////////////////////////////////////////////////
// MACROS
////////////////////////////////////////////////////////////////////////////
#define MAKE_DATA_INPUT		GPIODirModeSet(SHT11PORT, SHT_DATA, GPIO_DIR_MODE_IN)
#define MAKE_DATA_OUTPUT	GPIODirModeSet(SHT11PORT, SHT_DATA, GPIO_DIR_MODE_OUT)
#define SET_DATA			GPIOPinWrite  (SHT11PORT, SHT_DATA, SHT_DATA)
#define RESET_DATA			GPIOPinWrite  (SHT11PORT, SHT_DATA, 0)
#define SET_SCK				GPIOPinWrite  (SHT11PORT, SHT_SCK, SHT_SCK)
#define RESET_SCK			GPIOPinWrite  (SHT11PORT, SHT_SCK, 0)


////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES
////////////////////////////////////////////////////////////////////////////
typedef union {
	unsigned short i;
	float f;
} value;

enum {TEMP, HUMI};


/////////////////////////////////////////////////////////////////////////////////////////
// PROTOTYPES
/////////////////////////////////////////////////////////////////////////////////////////
char s_read_statusreg(unsigned char*, unsigned char*);
char s_write_statusreg(unsigned char*);
char s_softreset(void);
void delay_us(int);
unsigned char s_write_byte(unsigned char);
unsigned char s_read_byte(unsigned char);
void s_transstart(void);
void s_connectionreset(void);
unsigned char s_measure(unsigned char*, unsigned char*, unsigned char);
void calc_sth11(float* , float*);

#define _SHT11IF
#endif
